package com.evan.designpatterns.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author :liujiapeng01
 * @date :2019-03-13 20:13
 **/
public class ContainerSingleton {

    private ContainerSingleton() {
    }


    private static Map<String, Object> map = new ConcurrentHashMap<>();

    public static Object getInstance(String className) {

        if (!map.containsKey(className)) {
            synchronized (map) {
                Object instance = null;
                try {
                    instance = Class.forName(className).newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                map.put(className, instance);
                return instance;
            }
        } else {
            return map.get(className);
        }
    }

}
